package com.offcn.web.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.entity.Paging;
import com.offcn.entity.ResultVo;
import com.offcn.entity.User;
import com.offcn.service.UserService;
import com.offcn.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    private ResultVo resultVo = null;
    private UserService serviceImpl = new UserServiceImpl();
    //登录的方法
    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String imageCode = req.getParameter("imageCode");
        HttpSession session = req.getSession();
        String verCode = (String) session.getAttribute("verCode");
        if (imageCode.equalsIgnoreCase(verCode)){//通过验证码的验证
            User user = serviceImpl.login(username,password);
            if (user != null){//用户名密码正确
                if (user.getRole() == 1 && user.getStatus() == 1){// 1 为管理员有权限登录后台
                    session.setAttribute("user", user);//将登陆者的从数据库查到的信息存到session作用域中
                    resultVo = new ResultVo(1,"登陆成功！",user);
                }else {//普通用户没有权限登录后台
                    resultVo = new ResultVo(0,"您没有权限登录，请联系管理员！",null);
                }
            }else {//用户名或密码错误
                resultVo = new ResultVo(0,"用户名或密码错误！",null);
            }
        }else {//没有通过验证码的验证
            resultVo = new ResultVo(0, "验证码错误！", null);
        }
        String s = JSON.toJSONString(resultVo);//转换为json格式
        resp.getWriter().print(s);//响应到页面
    }

    //校验是否登录并且
    // 若登录则回显用户真实姓名
    //若没登录则回到login页面
    public void findUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        Object user = session.getAttribute("user");//如果登录了则session作用域里会有user对象
        if (user != null){
            resultVo = new ResultVo(1, "欢迎登录！", user);
        }else {
            resultVo = new ResultVo(0, "您尚未登录，请先登录！", null);
        }
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }

    //退出登录的方法，要清理session中的user对象
    public void loginOut(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.removeAttribute("user");
        resultVo = new ResultVo(1, "退出成功！", null);
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
        int i = Integer.parseInt("55");
    }

    //用户的分页查询方法
    public void findUserByPage(HttpServletRequest req,HttpServletResponse resp) throws SQLException, IOException {
        String pageSize = req.getParameter("pageSize");
        String currentPage = req.getParameter("currentPage");
        String usernameSearch = req.getParameter("usernameSearch");
        //先要查询数据的总条数
        int totalCount = serviceImpl.queryTotalCount(usernameSearch);
        //封装进分页协助类中
        Paging page = new Paging(currentPage, Integer.parseInt(pageSize),totalCount);
        //查询并且分页显示
        List<User> list = serviceImpl.findUserByPage(page,usernameSearch);
        page.setList(list);
        if (page.getList() != null && page.getList().size() > 0){
            resultVo = new ResultVo(1, "分页查询成功！",page);
        }else {
            resultVo = new ResultVo(0, "分页查询失败！", page);
        }
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }

    //修改用户的方法
    public void modifyUser(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException, SQLException {
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        BeanUtils.populate(user, map);
        int row = serviceImpl.modifyUser(user);
        if (row > 0){
            resultVo = new ResultVo(1, "修改成功！", row);
        }else {
            resultVo = new ResultVo(0, "修改失败！", row);
        }
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }
    //根据选择框所选进行删除，where id = ?
    public void DeleteAccordingSelection(HttpServletRequest req,HttpServletResponse resp) throws SQLException, IOException {
        String delarr = req.getParameter("delarr");
        //String[] split = delarr.split(",");
        //int row = serviceImpl.DeleteAccordingSelection(split);
        int row = serviceImpl.DeleteAccordingSelection(delarr);
        if (row > 0){
            resultVo = new ResultVo(1, "删除成功！", row);
        }else {
            resultVo = new ResultVo(0, "删除失败！", row);
        }
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }
    //新增用户
    public void addUser(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException, SQLException {
        String name = req.getParameter("name");
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        BeanUtils.populate(user, map);
        int row = serviceImpl.addUser(user);
        if (row > 0){
            resultVo = new ResultVo(1, "新增用户成功！", row);
        }else {
            resultVo = new ResultVo(0, "新增用户失败！", row);
        }
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }


}
